<script setup>
import { ref } from 'vue'
import {
  articleEdiChannelService,
  articleAddChannelService
} from '@/api/article'
const dialogVisible = ref(false)
/* 绑定校验 */
const formRef = ref()
const formModel = ref({
  articleSortTitle: ''
})
/* 验证 */
const rules = {
  articleSortTitle: [
    {
      required: true,
      message: '请输入分类名称',
      trigger: 'blur'
    },
    {
      pattern: /^\S{1,10}$/,
      message: '分类名必须是1-10位的非空字符',
      trigger: 'blur'
    }
  ]
}
/* 判断是修改还是新增 */
const emit = defineEmits(['success'])
const onsubmit = async () => {
  await formRef.value.validate()
  console.log(formModel.value)
  formModel.value.id
    ? await articleEdiChannelService(formModel.value)
    : await articleAddChannelService(formModel.value)
  ElMessage({
    type: 'success',
    message: formModel.value.id ? '编辑成功' : '添加成功'
  })
  dialogVisible.value = false
  emit('success')
}
/* 方法获取值 */
const open = async (row) => {
  dialogVisible.value = true
  formModel.value = { ...row }
}

defineExpose({
  open
})
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    :title="formModel.id ? '编辑分类' : '添加分类'"
    width="30%"
  >
    <el-form :model="formModel" ref="formRef" :rules="rules">
      <el-form-item
        label="分类名称"
        prop="articleSortTitle"
        label-width="100px"
        style="padding-right: 30px"
      >
        <el-input
          v-model="formModel.articleSortTitle"
          minlength="1"
          maxlength="10"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onsubmit"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
